<?php

namespace app\admin\controller;

use app\common\model\Shb as ShbModel;
use app\common\controller\AdminBase;
use think\Request;


class Shb extends AdminBase
{

    protected $model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new ShbModel();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        if (!$request->has("uid")){
            $uid = session("admin_id");
        }

        $info = $this->model->where("uid","=",$uid)->find();
        if ($info){
            $info = $info->toArray();
        }else{
            $info = [];
        }
        $info["sn"] = lock($uid);
        return $this->fetch("",$info);
    }

    /**
     * 下载升话宝主文件
     * @param Request $request
     */
    public function down_shb_main_file(Request $request)
    {

    }

    /**
     * 保存配置
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            if (!isset($data["uid"])){
                $data["uid"] = session("admin_id");
            }

            if (isset($data["id"]) && $data["id"]){
                if ($this->model->allowField(true)->save($data, ['id'=>$data['id']]) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }else{
                if ($this->model->allowField(true)->save($data) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }

            exit;
        }

    }


}
